1
//--------------------------------------------------------------------------
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 // File: TaskFactoryExtensions_FromAsync.cs
7 //--------------------------------------------------------------------------
9 namespace System
.Threading
.Tasks
11 /// <summary>Extensions for TaskFactory.</summary>
12 public static partial class TaskFactoryExtensions
14 /// <summary>Creates a Task that will be completed when the specified WaitHandle is signaled.</summary>
15 /// <param name="factory">The target factory.</param>
16 /// <param name="waitHandle">The WaitHandle.</param>
17 /// <returns>The created Task.</returns>
18 public static Task
FromAsync(this TaskFactory factory
, WaitHandle waitHandle
)
20 if (factory
== null) throw new ArgumentNullException("factory");
21 if (waitHandle
== null) throw new ArgumentNullException("waitHandle");
23 var tcs
= new TaskCompletionSource
<object>();
24 var rwh
= ThreadPool
.RegisterWaitForSingleObject(waitHandle
, delegate { tcs.TrySetResult(null); }
, null, -1, true);
26 t
.ContinueWith(_
=> rwh
.Unregister(null), TaskContinuationOptions
.ExecuteSynchronously
);